package com.sdms.core.login;

import java.util.HashMap;
import java.util.Map;

import com.sdms.core.rbac.Role;
import com.sdms.core.user.SystemUser;

public class UserStore implements IUserStore
{
	private static Map<String, SystemUser> userStore = new HashMap<String, SystemUser>();

	static
	{
		// Add default users for testing
		// username = uname1 password = password1
		userStore.put( "uname1", createSystemUser( "uname1", "test1 user", "7C6A180B36896A0A8C02787EEAFB0E4C", Role.CUSTOMER ) );
		// username = uname2 password = password2
		userStore.put( "uname2", createSystemUser( "uname2", "test2 user", "6CB75F652A9B52798EB6CF2201057C73", Role.ADMINISTRATOR ) );
		// username = user password = user321
		userStore.put( "user", createSystemUser( "user", "normal user", "F67C683F0F3E98CB9DD5582E8CBBCD04", Role.CUSTOMER ) );
		// username = customer password = customer321
		userStore.put( "customer", createSystemUser( "customer", "normal customer", "97BB4AAA5E58BA4EB1922F73F012AF28", Role.CUSTOMER ) );
		// username = admin password = admin321
		userStore.put( "admin", createSystemUser( "admin", "admin user", "4ACB4BC224ACBBE3C2BFDCAA39A4324E", Role.ADMINISTRATOR ) );
		// username = manager password = manager321
		userStore.put( "manager", createSystemUser( "manager", "manager", "8E1525C2FFFBE30AD9581C601EE4B1A8", Role.MANAGER ) );
		// username = agent password = agent321
		userStore.put( "agent", createSystemUser( "agent", "delivery agent", "9512D41AEFE6B9E15C85B1C458B79408", Role.DELIVERY_AGENT ) );

	}

	private static UserStore userStoreObj;

	private UserStore()
	{

	}

	public static synchronized UserStore getInstance()
	{
		if ( userStoreObj == null )
		{
			userStoreObj = new UserStore();
		}
		return userStoreObj;
	}

	@Override
	public void addNewUser( String userName, SystemUser userDetails )
	{
		userStore.put( userName, userDetails );
	}

	@Override
	public SystemUser viewUser( String userName )
	{
		return userStore.get( userName );
	}

	private static SystemUser createSystemUser( String uName, String fullName, String pwd, Role role )
	{
		SystemUser user = new SystemUser();
		user.setFullName( fullName );
		user.setPassWord( pwd );
		user.setUserName( uName );
		user.setRole( role.clone() );
		return user;
	}
}
